//
//  YHIMUIKitConversationViewController.m
//  YHChat
//
//  Created by apple on 2019/3/18.
//  Copyright © 2019 yinhe. All rights reserved.
//

#import "YHIMUIKitConversationViewController.h"
#import <Masonry/Masonry.h>

#import "YHIMUIKit.h"
#import "YHIMUIKitConvCellNode.h"
#import "YHIMUIKitConvCellData.h"

@interface YHIMUIKitConversationViewController () <ASTableDelegate, ASTableDataSource>
@property (nonatomic, strong) ASTableNode *tableNode;

@property (nonatomic, strong) NSMutableArray<YHIMUIKitConvCellData *> *dataSource;
@end

@implementation YHIMUIKitConversationViewController

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kYHIMUIKitConversationDidChangeNotification object:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.dataSource = [NSMutableArray array];
    
    [self.view addSubnode:self.tableNode];
    
    [self.tableNode.view mas_makeConstraints:^(MASConstraintMaker *make) {
        if (@available(iOS 11.0, *)) {
            make.left.equalTo(self.view.mas_safeAreaLayoutGuideLeft);
            make.right.equalTo(self.view.mas_safeAreaLayoutGuideRight);
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
        } else {
            make.left.equalTo(self.view);
            make.right.equalTo(self.view);
            make.top.equalTo(self.view);
            make.bottom.equalTo(self.view);
        }
    }];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(conversationDidChange:) name:kYHIMUIKitConversationDidChangeNotification object:nil];
}

#pragma mark ------------------ Notification ------------------
- (void)conversationDidChange:(NSNotification *)noti{
    self.dataSource = [NSMutableArray arrayWithArray:[YHIMUIKit sharedInstance].allConversationList];
    dispatch_async(dispatch_get_main_queue(), ^{
       [self.tableNode reloadData];
    });
}

#pragma mark ------------------ ASTableDataSource ------------------
- (NSInteger)tableNode:(ASTableNode *)tableNode numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}

- (ASCellNodeBlock)tableNode:(ASTableNode *)tableNode nodeBlockForRowAtIndexPath:(NSIndexPath *)indexPath{
    YHIMUIKitConvCellData *cellData = self.dataSource[indexPath.row];
    return ^ASCellNode *(void) {
        YHIMUIKitConvCellNode *cellNode = [[YHIMUIKitConvCellNode alloc] init];
        cellNode.cellData = cellData;
        return cellNode;
    };
}

#pragma mark ------------------ ASTableDelegate ------------------
- (void)tableNode:(ASTableNode *)tableNode didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableNode deselectRowAtIndexPath:indexPath animated:YES];
    YHIMUIKitConvCellData *cellData = self.dataSource[indexPath.row];
}

#pragma mark ------------------ Getter ------------------
- (ASTableNode *)tableNode{
    if (!_tableNode) {
        _tableNode = [[ASTableNode alloc] initWithStyle:UITableViewStylePlain];
        _tableNode.delegate = self;
        _tableNode.dataSource = self;
        _tableNode.view.tableFooterView = [[UIView alloc] init];
    }
    return _tableNode;
}

@end
